<?php
/**
 *CouponServices
 * @author tan bing
 * @date 2021-09-15 17:19
 */


namespace Modules\Coupon\Services\Admin;


use Illuminate\Http\Request;
use Modules\Coupon\Entities\Coupon;
use Modules\Coupon\Repositories\Contracts\CouponInterface;
use Modules\Coupon\Validate\Admin\CouponValidate;

class CouponServices extends BaseService
{

    /**
     * @var Request
     * @author tan bing
     */
    private $request;

    /**
     * @var CouponValidate
     * @author tan bing
     */
    private $validate;

    /**
     * @var CouponInterface
     * @author tan bing
     */
    private $couponInterface;

    /**
     * CouponServices constructor.
     * @param Request $request
     * @param CouponValidate $couponValidate
     * @param CouponInterface $couponInterface
     * @author tan bing
     * @date 2021-09-15 17:24
     */
    public function __construct(Request $request, CouponValidate $couponValidate, CouponInterface $couponInterface)
    {
        $this->request = $request;
        $this->validate = $couponValidate;
        $this->couponInterface = $couponInterface;
    }

    /**
     * 获取优惠券详情
     * @param string $id
     * @return mixed
     * @author tan bing
     * @date 2021-09-15 17:27
     */
    public function getCouponDetail($id = '')
    {
        !$id && $id = $this->request->input('id');
        return $this->couponInterface->findById($id);
    }

    /**
     * 获取优惠券列表  --分页
     * @return array
     * @author tan bing
     * @date 2021-09-15 17:29
     */
    public function getCouponPageData()
    {
        $param = $this->request->input();
        $query = $this->couponInterface->getPageData($param, $this->perPage());
        $count = $query->total();
        $data = $query->items();

        return compact('count', 'data');
    }

    /**
     * 获取优惠券列表
     * @return mixed
     * @author tan bing
     * @date 2021-09-15 17:49
     */
    public function getCouponList()
    {
        $param = $this->request->input();
        return $this->couponInterface->getCouponList($param);
    }

    /**
     * 添加或编辑优惠券信息
     * @return mixed
     * @throws \Exception
     * @author tan bing
     * @date 2021-09-15 17:49
     */
    public function addAndEditDetail()
    {
        $data = $this->request->post();
        $scene = !empty($data['id']) ? 'edit' : 'add';
        if(!$this->validate->scene($scene)->check($data)) {
            throw new \Exception($this->validate->getError());
        }
        try {
            if($data['coupon_type'] == 1) {
                if($data['reduce_price'] + 0 <= 0) throw new \Exception('减免金额必须大于0');
            } elseif($data['coupon_type'] == 2) {
                if($data['discount'] < 1) throw new \Exception('折扣不能低于1折');
                if($data['discount'] > 9.9) throw new \Exception('折扣不能大于9.9折');
            }
            $result = $this->couponInterface->addAndEditDetail($data);
        } catch (\Exception $e) {
            throw new \Exception($e->getMessage());
        }

        return $result;
    }

    /**
     * 修改状态
     * @return mixed
     * @throws \Exception
     * @author tan bing
     * @date 2021-09-15 17:51
     */
    public function editStatus()
    {
        $param = $this->request->post();
        if (!$this->validate->scene('status')->check($param)) {
            throw new \Exception($this->validate->getError());
        }
        $model = new Coupon();
        if (!$result = $model->editStatus($param, !empty($param['key']) ? $param['key'] : 'status')) throw new \Exception('操作失败');

        return $result;
    }

    /**
     * 删除
     * @return mixed
     * @author tan bing
     * @date 2021-09-15 17:51
     */
    public function delete()
    {
        $id     = $this->request->get('id', '');
        $model = new Coupon();
        return $model->forceDeleteData($id);
    }

    /**
     * 批量删除
     * @return mixed
     * @author tan bing
     * @date 2021-06-24 10:00
     */
    public function batchDelete()
    {
        $ids    = $this->request->get('ids', '');
        $model = new Coupon();
        return $model->batchForceDeleteData($ids);
    }

    /**
     * 获取用户优惠券列表
     * @return array
     * @author tan bing
     * @date 2021-09-16 11:26
     */
    public function getUserCouponList()
    {
        $param = $this->request->input();
        $query = $this->couponInterface->getUserCouponList($param, $this->perPage());
        $count = $query->total();
        $data = $query->items();

        return compact('count', 'data');
    }
}